#!/bin/bash
# curl -s https://gitee.com/yunwe/shell/raw/master/get_harbor_images.sh | sh

Harbor_Address=172.18.47.240       #Harbor主机地址
Harbor_User=admin                      #登录Harbor的用户
Harbor_Passwd=Harborzhxc.com1              #登录Harbor的用户密码
Images_File=harbor-images-`date '+%Y-%m-%d'`.txt   # 镜像清单文件                   
set -x
# 获取Harbor中所有的项目（Projects）
Project_List=$(curl -s -u ${Harbor_User}:${Harbor_Passwd}  -H "Content-Type: application/json" -X GET  http://${Harbor_Address}/api/v2.0/projects  -k  | python -m json.tool | grep name | awk '/"name": /' | awk -F '"' '{print $4}')

for Project in $Project_List;do
   # 循环获取项目下所有的镜像
    Image_Names=$(curl -s -u ${Harbor_User}:${Harbor_Passwd} -H "Content-Type: application/json" -X GET http://${Harbor_Address}/api/v2.0/projects/$Project/repositories -k | python -m json.tool | grep name | awk '/"name": /' | awk -F '"' '{print $4}')
    for Image in $Image_Names;do
        # 循环获取镜像的版本（tag)
        Image_Tags=$(curl -s -u ${Harbor_User}:${Harbor_Passwd}  -H "Content-Type: application/json"   -X GET  http://${Harbor_Address}/v2/$Image/tags/list  -k |  awk -F '"'  '{print $8,$10,$12}')
        for Tag in $Image_Tags;do
            # 格式化输出镜像信息
            echo "$Harbor_Address/$Image:$Tag"  > harbor-images-`date '+%Y-%m-%d'`.txt
        done
    done
done
